<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2018/12/27
 * Time: 20:11
 */

namespace app\admin\controller;

use think\Db;

class Amazonstore extends Main
{

    /**
     *店铺列表
     */
    public function store()
    {
        if (checkEntranceList()) {//总公司管理员，跳转选子公司页面
            action('admin/globalFun/choose', ['backtrack' => "store"]);
            return $this->fetch(APP_PATH . request()->module() . '/view/public/choose.html');
        }
        $where['company_id'] = iget('company_id') ? iget('company_id') : isession('company_id');
        $data = Db::name('amazon_store')
            ->where($where)
            ->order('id desc')
            ->paginate(12, false, ['query' =>
                [
                    'company_id' => iget('company_id')
                ]]);
        $this->assign('store', $data);
        return $this->fetch('store');
    }

    /**
     * 新建店铺
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function storeAdd()
    {
        if (post()) {
            $data = post();
            $data['org'] = trim($data['org']);
            $data['company_id'] = isession('company_id');
            $data['timestamp'] = time();
            DB::name('amazon_store')->insert($data);
            $this->success("添加成功");
        } else {
            if (checkEntranceAdd()) {
                $this->error("当前状态不允许添加信息");
            }
            $where['company_id'] = isession('company_id');
            $where['status'] = 1;

            $store = Db::name('amazon_store')->where($where)->find();
            $this->assign('store', $store);
            $this->assign('nation', $this->getDictionary('nation', "marketplace != ''"));
            return $this->fetch('storeAdd');
        }
    }

    /**
     * 编辑店铺
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function storeEdit($id)
    {
        if (post()) {
            $data = post();
            if (checkSubmitEdit()) {
                $this->error("当前状态不允许编辑信息");
            }
            $data['org'] = trim($data['org']);
            unset($data['id']);
            unset($data['company_id']);
            DB::name('amazon_store')->where('id', post('id'))->update($data);
            $this->success("更新成功");
        } else {
            $store = DB::name('amazon_store')->where('id', $id)->find();
            $this->assign("store", $store);
            $this->assign('nation', $this->getDictionary('nation', "marketplace != ''"));
            return $this->fetch('storeEdit');
        }
    }

    /**
     * 删除店铺
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function storeDel()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where = "id in (" . join(",", $data['id']) . ")";
        } else {
            $where = "id =" . $data['id'];
        }
        Db::name('amazon_store')->where($where)->delete();
        $this->success("删除成功");
    }

    /**
     * 分配用户
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function storeUser($id)
    {
        if (post()) {
            $data = post();
            if ($data['company_id'] != isession('company_id') && !empty($data['company_id'])) {
                $this->error("当前状态不允许编辑信息");
            }
            if (!isset($data['users']) || count($data['users']) == 0) {
                //删除该支付信息绑定用户
                $where['account_id'] = $data['id'];
                $where['type'] = 1;
                Db::name('store_user')->where($where)->delete();
                $this->success('修改成功');
            }
            //删除该支付信息绑定用户
            $where['account_id'] = $data['id'];
            $where['type'] = 1;
            Db::name('store_user')->where($where)->delete();
            foreach ($data['users'] as $key => $value) {
                $insertData[] = ['user_id' => $value, 'account_id' => $data['id']];
            }
            Db::name('store_user')->insertAll($insertData);
            $this->success('分配成功');
        } else {
            $company_id = iget('company_id') ? iget('company_id') : isession('company_id');
            $user = $this->getAllUser($company_id);
            //用户所有不重复角色id
            $role = [];
            if (checkArr($user)) {
                $role_id = join(',', array_unique(array_column($user, 'role_id')));
                $role = arrayCombine($this->getRoleById($role_id), 'id');
                foreach ($user as $key => $value) {
                    isset($role[$value['role_id']]) && $role[$value['role_id']]['user'][] = $value;
                }
            }
            //获取亚马逊店铺绑定用户
            $where['account_id'] = $id;
            $where['type'] = 1;
            $store = Db::name('store_user')
                ->where($where)
                ->select();
            $this->assign('user', arrayCombine($store, 'user_id'));
            $this->assign('role', $role);
            return $this->fetch('storeUser');
        }
    }

    /**
     * 批量修改
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function batch()
    {
        if (post()) {
            $data = post();
            Db::name('amazon_store')->where("status = 1 ")->update($data);
            $this->success("修改成功");
        } else {
            $store = Db::name('amazon_store')->where("seller_id !='' AND status = 1 ")->find();
            $this->assign('store', $store);
            return $this->fetch('batch');
        }
    }
}